<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>首页信息管理</el-breadcrumb-item>
      <el-breadcrumb-item>新闻列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getNewsList" @keyup.enter="getNewsList">
            <el-button slot="append" icon="el-icon-search" @click="getNewsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addNewsDialogVisible = true">添加新闻</el-button>
        </el-col>
      </el-row>

      <el-table :data="NewsList" border stripe>
        <el-table-column label="标题" prop="title"></el-table-column>
        <el-table-column label="简介" prop="info"></el-table-column>
        <el-table-column label="发布时间" prop="date"></el-table-column>
        <el-table-column label="作者" prop="author"></el-table-column>
        <el-table-column label="内容" prop="content"></el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">          
            <!-- 删除按钮 -->
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeNewsById(scope.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pageNo"
        :page-sizes="[1, 2, 5, 10]" :page-size="queryInfo.pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>

    <!-- 添加新闻的对话框 -->
    <el-dialog title="添加新闻" :visible.sync="addNewsDialogVisible" width="50%" @close="addNewsDialogClosed">
      <!-- 内容主体区域 -->
      <el-form :model="addNewsForm" :rules="addNewsFormRules" ref="addNewsFormRef" label-width="70px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="addNewsForm.title"></el-input>
        </el-form-item>
        <el-form-item label="简介" prop="info">
          <el-input v-model="addNewsForm.info"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input v-model="addNewsForm.content"></el-input>
        </el-form-item>
        <el-form-item label="作者" prop="author">
          <el-input v-model="addNewsForm.author"></el-input>
        </el-form-item>
        <el-form-item label="图片"   >        
          <input type="file"  @change="getFileadd($event)" />
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addNewsDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addNews">确 定</el-button>
      </span>

    </el-dialog>

  </div>
</template>

<script>
export default {
  data() {

    return {
      // 获取新闻列表的参数对象
      queryInfo: {
        pageNo: 1,
        // 当前每页显示多少条数据
        pageSize: 5
      },
      NewsList: [],
      total: 0,
      // 控制添加新闻对话框的显示与隐藏
      addNewsDialogVisible: false,
      // 添加新闻的表单数据
      addNewsForm: {
        id: '',
        title: '',
        info: '',
        content: '',
        author:'',
        file:''
      },
      fileList: [],
      // 添加表单的验证规则对象
      addNewsFormRules: {

      },

      // 所有角色的数据列表
      rolesList: [],
      userInfo: {},	
      // 已选中的角色Id值
      selectedRoleId: ''
    }
  },
  created() {
    this.getNewsList()
  },
  methods: {
    async getNewsList() {
        const { data: res } = await this.$http.get('news/newsList', {
            params: this.queryInfo
        })

        if (res.code !== 200) {
            return this.$message.error('获取新闻列表失败！')
        }
        this.NewsList = res.data.info
        this.total = res.data.total     
        console.log(this.NewsList)
    },
    // 监听 pageSize 改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize
      this.getNewsList()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.pageNo = newPage
      this.getNewsList()
    },
    // 监听添加新闻对话框的关闭事件
    addNewsDialogClosed() {
      this.$refs.addNewsFormRef.resetFields()
    },
    // 点击按钮，添加新新闻
    addNews() {
    	let form = new FormData()
      form.append('file',this.addNewsForm.file)
      form.append('title',this.addNewsForm.title)
      form.append('author',this.addNewsForm.author)
      form.append('info',this.addNewsForm.info)
      form.append('content',this.addNewsForm.content)  
      this.$refs.addNewsFormRef.validate(async valid => {
        if (!valid) return
              const { data: res } = await this.$http.post('news', form,{
                            headers:{
                                'Content-Type':'multipart/form-data'
                            },
                        })

              if (res.code !== 200) {
                  this.$message.error('添加新闻失败！')
              }

        this.$message.success('添加新闻成功！')
        // 隐藏添加新闻的对话框
        this.addNewsDialogVisible = false
        // 重新获取新闻列表数据
        this.getNewsList()
      })
    },
          getFileadd(event){
                this.addNewsForm.file = event.target.files[0]
                
           },
    // 展示编辑新闻的对话框

    // 根据Id删除对应的新闻信息
    async removeNewsById(id) {
      // 弹框询问新闻是否删除数据
      const confirmResult = await this.$confirm(
        '此操作将永久删除该新闻, 是否继续?',
        '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)

      // 如果新闻确认删除，则返回值为字符串 confirm
      // 如果新闻取消了删除，则返回值为字符串 cancel
      // console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }

          const {
              data: res
          } = await this.$http.delete('news/'+id )

          if (res.code !== 200) {
              return this.$message.error('删除新闻失败！')
          }

      this.$message.success('删除新闻成功！')
      this.getNewsList()
    }
  }
}
</script>

<style lang="less" scoped>
</style>
